package com.cjbdi.tree;

import java.util.ArrayList;
import java.util.List;

import com.cjbdi.entity.Fypz;

/**
 * 创建一个功能树，用于前端选择
 * @author xiandafu
 *
 */
public class FyTreeBuildUtil {
	
	private  FyTreeBuildUtil(){
		
	}
	public static FyTreeItem buildFyTree(List<Fypz> list){
		Fypz root = new Fypz();
		root.setId("-1");
		FyTreeItem rootOrg = new FyTreeItem(root);
		buildTreeNode(rootOrg,list);
		return rootOrg;
	}
	
	private static void buildTreeNode(FyTreeItem parent,List<Fypz> list){
		
		String id = parent.getSId();
		List<Fypz> dels = new ArrayList<>();
		for(Fypz SysFunction:list){
			if(SysFunction.getPid()!=null && SysFunction.getPid().equals(id)){
				FyTreeItem item = new FyTreeItem(SysFunction);
				item.setParent(parent);
				dels.add(SysFunction);
			}
		}
		list.removeAll(dels);
		
		if(list.isEmpty()){
			return ;
		}
		for(FyTreeItem child:parent.getChildren()){
			buildTreeNode(child,list);
		}
		
	}
}
